﻿Public Class FrmLabel
    Private Sub FrmLabel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Icon = FrmMain.Icon

        Dim i As Integer
        LvwSmart.Items.Clear()
        LvwCustom.Items.Clear()
        If FrmSetting.LastSelect < 0 Then
            Close()
        ElseIf FrmSetting.JsonSetting(FrmSetting.LastSelect)("label").Count = 0 Then
            Exit Sub
        End If
        For i = 0 To FrmSetting.JsonSetting(FrmSetting.LastSelect)("label").Count - 1
            If i = 0 Then
                Dim j As Integer
                Dim s As String = FrmSetting.JsonSetting(FrmSetting.LastSelect)("label")(i).ToString
                Dim a() As String = s.Split("|")
                For j = 0 To a.Count - 1
                    LvwSmart.Items.Add(a(j))
                Next
            Else
                LvwCustom.Items.Add(FrmSetting.JsonSetting(FrmSetting.LastSelect)("label")(i).ToString)
            End If
        Next
    End Sub

    Private Sub FrmLabel_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        LvwSmart.Location = New Point(10, 10)
        LvwSmart.Size = New Size(BtnUp.Width * 5 + 20, LvwSmart.Height)
        LvwCustom.Location = New Point(10, LvwSmart.Bottom + 5)
        LvwCustom.Size = New Size(LvwSmart.Width, ClientSize.Height - LvwSmart.Height - BtnAll.Height - BtnUp.Height - 35)
        BtnUp.Location = New Point(10, LvwCustom.Bottom + 5)
        BtnDown.Location = New Point(BtnUp.Right + 5, BtnUp.Top)
        BtnNew.Location = New Point(BtnDown.Right + 5, BtnUp.Top)
        BtnEdit.Location = New Point(BtnNew.Right + 5, BtnUp.Top)
        BtnDel.Location = New Point(BtnEdit.Right + 5, BtnUp.Top)
        BtnAll.Location = New Point(10, BtnUp.Bottom + 5)
        BtnReverse.Location = New Point(BtnAll.Right + 5, BtnAll.Top)
        BtnCancel.Location = New Point(BtnReverse.Right + 5, BtnAll.Top)
        BtnAdd.Location = New Point(BtnCancel.Right + 5, BtnAll.Top)
        BtnAdd.Size = New Size(LvwCustom.Right - BtnCancel.Right - 5, BtnAdd.Height)
    End Sub

    Private Sub BtnUp_Click(sender As Object, e As EventArgs) Handles BtnUp.Click
        If IsNothing(LvwCustom.FocusedItem) Then Exit Sub
        If LvwCustom.FocusedItem.Index > 0 Then
            Dim s As String
            s = LvwCustom.Items(LvwCustom.FocusedItem.Index - 1).Text
            LvwCustom.Items(LvwCustom.FocusedItem.Index - 1).Text = LvwCustom.Items(LvwCustom.FocusedItem.Index).Text
            LvwCustom.Items(LvwCustom.FocusedItem.Index).Text = s
            LvwCustom.Items(LvwCustom.FocusedItem.Index - 1).Selected = True
            LvwCustom.Items(LvwCustom.FocusedItem.Index - 1).Focused = True
        End If
    End Sub

    Private Sub BtnDown_Click(sender As Object, e As EventArgs) Handles BtnDown.Click
        If IsNothing(LvwCustom.FocusedItem) Then Exit Sub
        If LvwCustom.FocusedItem.Index < LvwCustom.Items.Count - 1 Then
            Dim s As String
            s = LvwCustom.Items(LvwCustom.FocusedItem.Index + 1).Text
            LvwCustom.Items(LvwCustom.FocusedItem.Index + 1).Text = LvwCustom.Items(LvwCustom.FocusedItem.Index).Text
            LvwCustom.Items(LvwCustom.FocusedItem.Index).Text = s
            LvwCustom.Items(LvwCustom.FocusedItem.Index + 1).Selected = True
            LvwCustom.Items(LvwCustom.FocusedItem.Index + 1).Focused = True
        End If
    End Sub

    Private Sub BtnNew_Click(sender As Object, e As EventArgs) Handles BtnNew.Click
        Dim s As String
        Static l As String = ""
        s = InputBox("请输入一段文本：", "提示", l)
        l = s
        If s <> "" Then
            If s.IndexOf(",") <> -1 Then
                MsgBox("标签名称不能包含半角逗号(,)字符。", vbExclamation, "错误")
            ElseIf ListViewExists(LvwSmart, s) Or ListViewExists(LvwCustom, s) Then
                MsgBox("标签已存在，请重新输入。", vbExclamation, "错误")
            Else
                LvwCustom.Items.Add(s)
            End If
        End If
    End Sub

    Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
        Dim s As String
        s = InputBox("请输入一段文本：", "提示", LvwCustom.FocusedItem.Text)
        If s <> "" Then
            If s.IndexOf(",") <> -1 Then
                MsgBox("标签名称不能包含半角逗号(,)字符。", vbExclamation, "错误")
            Else
                LvwCustom.FocusedItem.Text = s
            End If
        End If
    End Sub

    Private Sub BtnDel_Click(sender As Object, e As EventArgs) Handles BtnDel.Click
        Dim i As Integer
        For i = LvwCustom.Items.Count - 1 To 0 Step -1
            If LvwCustom.Items(i).Checked = True Then
                LvwCustom.Items(i).Remove()
            End If
        Next
    End Sub

    Private Sub BtnAll_Click(sender As Object, e As EventArgs) Handles BtnAll.Click
        Dim i As Integer
        For i = 0 To LvwCustom.Items.Count - 1
            LvwCustom.Items(i).Checked = True
        Next
    End Sub

    Private Sub BtnReverse_Click(sender As Object, e As EventArgs) Handles BtnReverse.Click
        Dim i As Integer
        For i = 0 To LvwCustom.Items.Count - 1
            LvwCustom.Items(i).Checked = Not LvwCustom.Items(i).Checked
        Next
    End Sub

    Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles BtnCancel.Click
        Dim i As Integer
        For i = 0 To LvwCustom.Items.Count - 1
            LvwCustom.Items(i).Checked = False
        Next
    End Sub

    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim i As Integer
        Dim s As String = ""
        If LvwSmart.Items.Count > 0 Then
            For i = 0 To LvwSmart.Items.Count - 1
                s += LvwSmart.Items(i).Text + IIf(i = LvwSmart.Items.Count - 1, "", "|")
            Next
        End If
        s += ","
        If LvwCustom.Items.Count > 0 Then
            For i = 0 To LvwCustom.Items.Count - 1
                s += LvwCustom.Items(i).Text + ","
            Next
        End If
        If s.Substring(s.Length - 1) = "," Then
            FrmSetting.TxtLabel.Text = s.Substring(0, s.Length - 1)
        Else
            FrmSetting.TxtLabel.Text = s
        End If

        FrmSetting.ListView.Items(FrmSetting.LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
        FrmSetting.ListView.Items(FrmSetting.LastSelect).Text = FrmSetting.JsonSetting(FrmSetting.LastSelect)("name").ToString + " [已修改]"
        If FrmSetting.TxtLabel.Text = "" Then
            FrmSetting.JsonSetting(FrmSetting.LastSelect)("label") = New Newtonsoft.Json.Linq.JArray
        Else
            Dim a As Newtonsoft.Json.Linq.JArray = New Newtonsoft.Json.Linq.JArray From {
                    Split(FrmSetting.TxtLabel.Text, ",")
                }
            FrmSetting.JsonSetting(FrmSetting.LastSelect)("label") = a
        End If
        Close()
    End Sub
End Class
